/*
 * Object pool code tutorial from
 * http://sourcemaking.com/design_patterns/object_pool
 *
 * Code used under non-commercial licence
 *
 *
 * DB.java makes use of the Object Pool OO design pattern.
 * This greatly speeds up the program and allows us to
 * reuse DB connections.
 */
package udio;
/**
 *
 * @author steve
 */
import java.sql.*;
public class DB extends DBConnectionPool<Connection> {

  private String dsn, usr, pwd;

  public DB() {
    super();
    String driver = "com.mysql.jdbc.Driver";
    try {
      Class.forName(driver).newInstance();
    } catch (Exception e) {
      e.printStackTrace();
    }
    this.dsn = "jdbc:mysql://sql09.freemysql.net/seng330project";
    this.usr = "seng330";
    this.pwd = "fightingchickensco";
  }

  @Override
  protected Connection create() {
    try {
      return (DriverManager.getConnection(dsn, usr, pwd));
    } catch (SQLException e) {
      e.printStackTrace();
      return (null);
    }
  }

  @Override
  public void expire(Connection o) {
    try {
      ((Connection) o).close();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  @Override
  public boolean validate(Connection o) {
    try {
      return (!((Connection) o).isClosed());
    } catch (SQLException e) {
      e.printStackTrace();
      return (false);
    }
  }

  public static void putQuery(Connection conn, String query) {
        try {
            Statement s = conn.createStatement ();
            s.executeUpdate (query);
            s.close();
        }
        catch (Exception e) {
            System.out.println( e.getMessage());
        }
    }

    public static ResultSet getQuery(Connection conn, String query) {
        ResultSet results = null;
        try {
            Statement s = conn.createStatement ();
            s.executeQuery (query);
            results = s.getResultSet ();
            //s.close();
        }
        catch (Exception e) {
            System.out.println( e.getMessage());
        }
        return results;
    }
}
/*public class DB {

    public Connection connect() {
        Connection conn = null;
        try
        {
            //create driver for MYSQL
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            //Details for DB connection
            String username = "seng330";
            String password = "fightingchickensco";

            //URL for DB connection
            String dbURL = "jdbc:mysql://sql09.freemysql.net/seng330project";

            //create the connection
             conn =  DriverManager.getConnection(dbURL, username, password);
        }
        catch( Exception e )
        {
                System.out.println( e.getMessage());
        }
        return conn;
    }

    public void disconnect(Connection conn) {
        try {
            conn.close();
        }
        catch (Exception e) {
            //ignore disconnection erros
        }
    }

    public static void putQuery(Connection conn, String query) {
        try {
            Statement s = conn.createStatement ();
            s.executeUpdate (query);
            s.close();
        }
        catch (Exception e) {
            System.out.println( e.getMessage());
        }
    }

    public static ResultSet getQuery(Connection conn, String query) {
        ResultSet results = null;
        try {
            Statement s = conn.createStatement ();
            s.executeQuery (query);
            results = s.getResultSet ();
            //s.close();
        }
        catch (Exception e) {
            System.out.println( e.getMessage());
        }
        return results;
    }
}
*/